package com.example.admin.yuejian_22.Fragment;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;


import com.example.admin.yuejian_22.Activity.LoginActivity;
import com.example.admin.yuejian_22.Activity.MyGZActivity;
import com.example.admin.yuejian_22.Activity.MyOrderActivity;
import com.example.admin.yuejian_22.Activity.MyPhotoActivity;
import com.example.admin.yuejian_22.Activity.MysetActivity;
import com.example.admin.yuejian_22.Activity.MyAppIntroActivity;
import com.example.admin.yuejian_22.Bean.UserBean;
import com.example.admin.yuejian_22.Listener.UserListener;
import com.example.admin.yuejian_22.Model.UserModel;
import com.example.admin.yuejian_22.R;
import com.squareup.picasso.Picasso;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends Fragment {
    private TextView my_name;
    private ImageView my_thum;
    private RadioButton my_order;
    private RadioButton my_focus;
    private RadioButton button_set;
    private RadioButton button_exit;
    private RadioButton button_about;
    private RadioButton my_shaitu;
    ProgressDialog loadingDialog;
    private static int MSG_DISMISS_DIALOG=0;

    private String userid;
    UserListener userListener = new UserListener() {
        @Override
        public void onResponse(Object beanobject) {
            my_name.setText(((UserBean)beanobject).getUser_name());

            String address="http://172.24.10.175/workout/Uploads/"+((UserBean)beanobject).getPhoto();
            if(!"".equals(((UserBean)beanobject).getPhoto())){
                Picasso.with(getActivity())
                        .load(address)
                        .placeholder(R.drawable.ic_my)
                        .noFade()
                        .into(my_thum);
            }
        }
        @Override
        public void onFail(String msg){
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        my_name = (TextView) view.findViewById(R.id.my_name);
        my_thum = (ImageView) view.findViewById(R.id.my_thum);
        my_focus = (RadioButton) view.findViewById(R.id.my_focus);
        my_focus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MyGZActivity.class);
                startActivity(intent);
            }
        });
        my_shaitu = (RadioButton) view.findViewById(R.id.my_shaitu);
        my_shaitu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MyPhotoActivity.class);
                startActivity(intent);
            }
        });
        my_order = (RadioButton) view.findViewById(R.id.my_order);
        my_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MyOrderActivity.class);
                startActivity(intent);
            }
        });
        button_about = (RadioButton) view.findViewById(R.id.button_about);
        button_about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MyAppIntroActivity.class);
                startActivity(intent);
            }
        });
        button_exit = (RadioButton) view.findViewById(R.id.button_exit);
        button_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showListDialog();
            }
        });
        button_set = (RadioButton) view.findViewById(R.id.button_set);
        button_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(),MysetActivity.class);
                startActivity(intent);
            }
        });
        userid = getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("user_id", String.valueOf(0));
        Log.v("获取id++++++++++", String.valueOf(userid));
        showData();

        return view;
    }
    private void showData() {
        UserModel userModel = new UserModel();
        userModel.getUserInfo(null, userid, userListener);
    }

    private void showListDialog() {
        final String[] items = {"注销"};
        AlertDialog.Builder listDialog = new AlertDialog.Builder(getActivity());
        listDialog.setTitle("个人设置");

        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                 which 下标从0开始
//                 ...To-do
//                Toast.makeText(getActivity(), "你点击了" + items[which], Toast.LENGTH_SHORT).show();
                showLoadingDialog();
                //延迟一秒钟关闭loadingDialog
                mHandler.sendEmptyMessageDelayed(MSG_DISMISS_DIALOG, 1000);
//                loadingDialog.dismiss();
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                getActivity().startActivity(intent);
                getActivity().finish();
            }
        });
        listDialog.show();
    }

    private Handler mHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);

            if(MSG_DISMISS_DIALOG == msg.what){


                if(null != loadingDialog){

                    if(loadingDialog.isShowing()){
                        // loadingDialog.dismiss();
                    }
                }
            }
        }
    };

    private void showLoadingDialog() {
        loadingDialog = new ProgressDialog(getActivity());
//        loadingDialog.setTitle("test");
        loadingDialog.setMessage("正在退出...");
        loadingDialog.setIndeterminate(true);
        loadingDialog.setCancelable(false);
        loadingDialog.show();
    }
}
